package com.zyf.connotation.ui

/**
 * 作者：Think
 * 创建于 2017/5/11 16:25
 */
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v7.app.ActionBarDrawerToggle
import android.view.Gravity
import android.view.KeyEvent
import cn.feng.skin.manager.base.SkinBaseActivity
import com.zyf.connotation.BaseFragment
import com.zyf.connotation.R
import com.zyf.connotation.fragment.ConnotationFragment
import com.zyf.connotation.fragment.VideoConnatationFragment
import connotation.ui.SkinActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : SkinBaseActivity(){
    var adapter:PagerAdapter?=null
    var fragment= ArrayList<BaseFragment>()
    val titles = listOf("段子","视频")
    lateinit var video:VideoConnatationFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        val toggle = ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.app_name, R.string.app_name)
        toggle.syncState()
        drawerLayout.addDrawerListener(toggle)
        setSupportActionBar(toolbar)
        toolbar.setNavigationOnClickListener{
            if (drawerLayout.isDrawerOpen(Gravity.START))
                drawerLayout.closeDrawer(Gravity.START)
            else
                drawerLayout.openDrawer(Gravity.START)
        }
        fragment.add(ConnotationFragment())
        video = VideoConnatationFragment()
        fragment.add(video)
        adapter = PagerAdapter(supportFragmentManager)
        viewPage.adapter = adapter
        tab.setupWithViewPager(viewPage)
        dynamicAddSkinEnableView(toolbar, "background", R.color.colorPrimary)
        dynamicAddSkinEnableView(tab, "background", R.color.colorPrimary)
        dynamicAddSkinEnableView(navigation.getHeaderView(0), "background", R.color.colorPrimary)
        dynamicAddSkinEnableView(tab, "tabIndicatorColor", R.color.colorAccent)

        navigation.setNavigationItemSelectedListener{
           when (it.itemId) {
                R.id.skin -> {
                    var intent = Intent(this@MainActivity, SkinActivity::class.java)
                    startActivity(intent)
                }
            }
            drawerLayout.closeDrawer(Gravity.START)
            return@setNavigationItemSelectedListener true
        }

    }

    inner class PagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
        override fun getItem(position: Int): BaseFragment {
            return fragment[position]
        }
        override fun getCount(): Int {
            return titles.size
        }
        override fun getPageTitle(position: Int): CharSequence {
            return titles[position]
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(viewPage.currentItem==1){
            if(video.back()){
                return true
            }else{
                return super.onKeyDown(keyCode, event)
            }
        }
        return super.onKeyDown(keyCode, event)
    }

}